import { Component } from 'react';

import { Toast, Button, Group, TextareaField} from 'saltui';
import DB from '../../../app/db';
import './PageHome.less';
const {
	Count
} = TextareaField;

export default class PageHome extends Component {


 constructor(props) {
    super(props);

    //关闭加载页的遮罩效果
    $(".loading-containter").hide();
    
    let id = this.props.params.id;
    this.state = {
      id: id,
      reason:''
    };

    var thirdMenuType = getLoginUserInfo().thirdMenuType;
    if(thirdMenuType == 'DING'){
      dd.biz.navigation.setTitle({
        title : "请假原因",//控制标题文本，空字符串表示显示默认文本
        onSuccess : function(result) {},
        onFail : function(err) {}
      });
    }else if(thirdMenuType =='MH'){
      !!emp&&emp.window.setTitle({
        title: {
            text: "请假原因",
            icon: '',
            actionmenu: {}
        },
      });
    }

    let t = this;
    let userId = document.getElementById("userHiddenId").value;
    var isHelpNew = 1;
    dd.util.domainStorage.getItem({ //帮助按钮
       name:'meeting_helpNew_'+userId , // 存储信息的key值
       onSuccess : function(info) {
            var helpNew = info.value;
            if( typeof(helpNew) == 'undefined' || helpNew == '' ){
              isHelpNew = 2;
            }
            //初始化按钮
            t.initButton(isHelpNew);
       },
       onFail : function(err) {
            //alert(JSON.stringify(err));
       }
    });
  }

  //初始化按钮
  initButton(isHelpNew){
    //设置帮助
    /*dd.biz.navigation.setIcon({
        showIcon : true,//是否显示icon
        iconIndex : isHelpNew,//显示的iconIndex,如上图
        onSuccess : function(result) {
            location.hash='help/help_index';
        },
        onFail : function(err) {
        //jsapi调用失败将会回调此函数
        }
    });*/
  }

  componentDidUpdate (){

    
  }

  
  //文本框输入
  handleTextChange(field, newValue, size) {
    if (newValue.length <= size) {
      this.setState({
        [field]: newValue,
      });
    }
  }
  
  submit(){
    if (this.state.reason.trim()=='') {
      Toast.show({
              type: 'error',
              content: '请填写请假原因',
              onHide: function () {
              }
          });
      return;
    }
    if(this.state.reason != null && isEmojiCharacter(this.state.reason)){
      Toast.show({
        type: 'error',
        content: '请假原因不支持表情输入',
        onHide: function () {}
      });
      return;
    }
    let param = {
      id: this.state.id,
      status:'2',
      reason:this.state.reason
    }

    DB.SomeModuleAPI.editNoticeInfo(param)
        .then((content) => {

          Toast.show({
          type: 'success',
              content: '提交成功'
           });

          //返回至列表页面
          setTimeout(function(){
            history.back();
           // backPage();
          },1000);
        }).catch((error) => {
          
            if(typeof(error.errorMsg) == 'undefined'){
              Toast.show({
              type: 'error',
              content: '提交出错'
               });
            }else{
              Toast.show({
              type: 'error',
              content: error.errorMsg
               });
            }
        });

  }

  cancel(){
    history.back();
  }

  render() {
    let t = this;
    return (
      <div>
          <div>
            <Group.List >
              <TextareaField
                required
                label="请假原因" minRows={6} maxRows={10}
                placeholder="请输入请假原因"
                value={t.state.reason}
                onChange={(value) => { t.handleTextChange('reason', value, 200); }}>
                <Count total={200} length={t.state.reason.length} />
              </TextareaField>
            </Group.List>
          </div>

          <div >
              <Button.Group >
                  <Button type="primary" display="inline" onClick={this.submit.bind(this)}>提交</Button>
                  <Button type="secondary" display="inline" onClick={this.cancel.bind(this)}>取消</Button>             
              </Button.Group>
          </div>
        
    </div>);
  }
}


